<?php
class TBB_Auth_Adapter extends Zend_Auth_Adapter_DbTable
{
	public function __construct($db = null, $tableName = null, $identityColumn = null, $credentialColumn = null,
	 $credentialTreatment = nul, $identity, $credential)
	{
		parent::__construct($db, $tableName, $identityColumn, $credentialColumn, $credentialTreatment);
		$this->setIdentity($identity);
		$this->setCredential($credential);
	}
	
	public function doAuthenticate()
	{
		// authenticate
		$auth = Zend_Auth::getInstance();
		$result = $auth->authenticate($this);
		if($result->isValid()) {
			// get username
			$username = $this->getResultRowObject(array('username'))->username;
			$customerModel = new Customer_Model_Customer();
			$customerName = $customerModel->getCustomerNameByUsername($username);
			
			// create object to serialize it into session 'Zend_Auth'
			$obj = new stdClass();
			$obj->username = $username;
			$obj->customerName = $customerName;
			
			// store username, name of the user		
			$storage = $auth->getStorage();
			$storage->write($obj);
			return true;
		}
		return false;

	}	
}